import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home.vue'
import Login from '../components/Login.vue'
import User from '../components//menus/User.vue'
import Right from '../components//menus/Right.vue'
import Good from '../components//menus/Good.vue'
import Order from '../components//menus/Order.vue'
import Setting from '../components//menus/Setting.vue'
import userDetail from '../components/user/UserDetail.vue'
Vue.use(VueRouter)
const router = new VueRouter({
  routes:[
    {path:'/',redirect:'/home'},
    {
      path:'/home',
      component:Home,
      children:[
        {path:'userDetail',component:userDetail},
        {path:'user',component:User},
        {path:'right',component:Right},
        {path:'good',component:Good},
        {path:'order',component:Order},
        {path:'setting',component:Setting}
      ]
    },
    {path:'/login',component:Login}
  ]
})


router.beforeEach((to,from,next)=>{
  if(to.path === '/login'){
    next()
  }else{
    const token = localStorage.getItem('token')
    if(token){
      next()
    }else{
      next('/login')
    }
  }
})
export default router